package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CompoundButton;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.ui.adapter.holder.ShortVideoHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.SpecialFrgHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.VideoPlayHolder;
import com.sctvcloud.dongqu.ui.util.IListShowData;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.UIUtils;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 短视频列表适配
 */
public class ShortVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//    private int mTemplate;//1：首条新闻大图其他新闻小图，2：全部新闻大图
//    public static final int VIEW_TYPE_SMALL = 0;
//    public static final int VIEW_TYPE_LARGE = 1;
//    private boolean isAct = false;

    private Context context;
    private List<NewsItem> dataList = new ArrayList<>();
    private OnVideoClickListener listener;

    public ShortVideoAdapter(Context context, OnVideoClickListener listener) {
        this.context = context;
        this.listener = listener;
    }


    @Override
    public ShortVideoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_short_video, parent, false);
        ShortVideoHolder holder = new ShortVideoHolder(view);
        return holder;
    }

    public void setData(List<NewsItem> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<NewsItem> list) {
        int pos = getItemCount();
        if (null != list) {
            dataList.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos, getItemCount());
        }
    }

    public void updateData(List<NewsItem> list) {
        if (null != list) {
            dataList = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NewsItem info = dataList.get(position);
        final ShortVideoHolder viewHolder = (ShortVideoHolder) holder;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        ViewGroup.LayoutParams lp = viewHolder.img.getLayoutParams();
        int imgWidth = (width - UIUtils.dip2px(context, 7 * 2 + 2 * 10)) / 2;
        lp.height = imgWidth * 9 / 16;
        viewHolder.img.setLayoutParams(lp);

        if (position == 0 || position == 1) {
            viewHolder.topDivider.setVisibility(View.VISIBLE);
        } else {
            viewHolder.topDivider.setVisibility(View.GONE);
        }
        viewHolder.title.setText(TextUtils.isEmpty(info.getNewsTitle()) ? "" : info.getNewsTitle());
        if (!TextUtils.isEmpty(info.getNewsImage())) {
            GlideUtil.getGlid(context, info.getNewsImage()).centerCrop().placeholder(R.mipmap.icon_def_16_9_s).error(R.mipmap.icon_def_16_9_s).into(viewHolder.img);
        }
//        if (mNewsItem.isDigg()) {
//            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_press);
//            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//            digg.setDrawables(drawable, null, null, null);
//            digg.setTextColor(context.getResources().getColor(R.color.colorRedMain));
//        } else {
//            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
//            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//            digg.setDrawables(drawable, null, null, null);
//            digg.setTextColor(context.getResources().getColor(R.color.colorOpinionSelectNo));
//        }
        viewHolder.digg.setText("" + info.getCountDigg());
        viewHolder.videoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.OnVideoClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnVideoClickListener {
        void OnVideoClick(int position);
    }
}
